﻿using System;
using System.Windows.Forms;
using Business;
using Business.Entity.Contabil;
using Business.Entity.Contabil.Pessoa;
using Business.Entity.Contabil.Pessoa.SubClass.Usuario.ClassesRelacionadas;
using Business.Forms;
using Business.Seguranca;
using Business.Validation;
using DevExpress.XtraEditors;

namespace Bonesoft.Forms.Contabil
{
    public partial class FormLancamento : FormDefault, IForm
    {
        public FormLancamento()
        {
            InitializeComponent();
            
            // Configurações dos botões de comando.
            cmdCancelar.Form = this;
            cmdExcluir.Form = this;
            cmdSalvar.Form = this;

            pessoaBindingSource.DataSource = LancamentoRepository.GetSession().CreateCriteria<Pessoa>().List();
            tipoLancamentoBindingSource.DataSource = LancamentoRepository.GetSession().CreateCriteria<TipoLancamento>().List(); ;
            InitFocusControl = dataLancamentoDateEdit;
        }

        public object Novo()
        {
            Lancamento l = new Lancamento();
            lancamentoBindingSource.DataSource = l;
            dataLancamentoDateEdit.Focus();
            ShowDialog();
            if (l.Id == 0)
            {
                return null;
            }
            return l;
        }

        public void Alterar(object entity)
        {
            lancamentoBindingSource.DataSource = entity;
            dataLancamentoDateEdit.Focus();
            ShowDialog();
        }

      
        private void cmdNovaPartida_Click(object sender, System.EventArgs e)
        {
        }

        public object Save()
        {
            lancamentoBindingSource.EndEdit();
            if (!Validate().HasError)
            {
                LancamentoRepository.Save((Lancamento)lancamentoBindingSource.Current);
            }
            return CurrentObject;
        }

        public void Update(object entity)
        {
            Alterar(entity);
        }

        public bool Delete(object entity)
        {
            CurrentObject = entity;
            LancamentoRepository.Delete((Lancamento) CurrentObject);
            return true;
        }

        public object CurrentObject
        {
            get
            {
                lancamentoBindingSource.EndEdit();
                return lancamentoBindingSource.DataSource;
            }
            set
            {
                lancamentoBindingSource.DataSource = value;
            }
        }

        public PermissaoForm Permissao
        {
            get
            {
                return SegurancaUtils.GetPermissaoForm(Name);
            }
        }

        public EntityValidationResult Validate()
        {
            var lanc = (Lancamento) CurrentObject;
            var result = DataValidation.ValidateEntity(lanc);
            foreach (var partida in lanc.Partidas)
            {
                foreach (var error in DataValidation.ValidateEntity(partida).ValidationErrors)
                {
                    result.ValidationErrors.Add(error);
                }
            }
            return result;
        }

        public void Reload()
        {
            
        }

        public void Cancel()
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        public Object New()
        {
            return Novo();
        }

        public XtraForm Form
        {
            get
            {
                return this;
            }
        }

        public FormState FormState { get; set; }
    }
}